import { isInArray, unzipPosition, zipPosition } from "@/utils"

export default{
    /* 过道行为 */
    cross:{
        // 初始化过道任务
        init(roomName:string,relateRoom:string[]):string{
            relateRoom = ['start'].concat(relateRoom)
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[cross] 不存在房间${roomName}`
            if (thisRoom.controller.level < 8) return `[cross] 房间${roomName}控制器等级不足！`
            var thisTask:MissionModel = {
                name:"过道采集",
                range:'Room',
                delayTick:99999,
                data:{
                    power:false,
                    deposit:false,
                    relateRooms:relateRoom
                }
            }
            if (thisRoom.AddMisson(thisTask)) return `[cross] 房间${roomName}初始化过道采集任务成功！ 房间：${relateRoom}`
            else return `[cross] 房间${roomName}初始化过道采集任务失败！请检查房间内是否已经存在该任务！`
        },
        // active power
        power(roomName:string):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[cross] 不存在房间${roomName}`
            for (var i of thisRoom.memory.Misson['Room'])
            {
                if (i.name == '过道采集')
                {
                    i.data.power = !i.data.power
                    return `[cross] 房间${roomName}过道采集任务的power属性已经更改为${i.data.power}`
                }
            }
            return `[cross] 房间${roomName}更改过道采集任务power属性失败！请检查房间内是否已经存在该任务！`
        },
        deposit(roomName:string):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[cross] 不存在房间${roomName}`
            for (var i of thisRoom.memory.Misson['Room'])
            {
                if (i.name == '过道采集')
                {
                    i.data.deposit = !i.data.deposit
                    return `[cross] 房间${roomName}过道采集任务的power属性已经更改为${i.data.deposit}`
                }
            }
            return `[cross] 房间${roomName}更改过道采集任务deposit属性失败！请检查房间内是否已经存在该任务！`
        },
        room(roomName:string,roomData:string[]):string{
            roomData = ['start'].concat(roomData)
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[cross] 不存在房间${roomName}`
            for (var i of thisRoom.memory.Misson['Room'])
            {
                if (i.name == '过道采集')
                {
                    i.data.relateRooms =roomData
                    return `[cross] 房间${roomName}过道采集任务的房间已经更改为${roomData}`
                }
            }
            return `[cross] 房间${roomName}更改过道采集任务deposit属性失败！请检查房间内是否已经存在该任务！`
        },
        /* 删除某个房间 */
        remove(roomName:string,delRoomName:string):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[cross] 不存在房间${roomName}`
            for (var i of thisRoom.memory.Misson['Room'])
            {
                if (i.name == '过道采集')
                {
                    /* 进行删除 */
                    for (var j of i.data.relateRooms)
                    {
                        if (j == delRoomName)
                        {
                            var list = i.data.relateRooms as string[]
                            var index = list.indexOf(j)
                            list.splice(index,1)
                            return `[cross] 房间${roomName}的过道采集清单里已经删除房间${j}！ 现有房间列表为${i.data.relateRooms}`
                        }
                    }
                    return `[cross] 房间${roomName}过道采集任务的房间清单未找到房间${delRoomName}`
                }
            }
            return `[cross] 房间${roomName}更改过道采集任务房间清单失败！请检查房间内是否已经存在该任务！`
        },
        /* 增加某个房间s */
        add(roomName:string,addRoomName:string):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[cross] 不存在房间${roomName}`
            for (var i of thisRoom.memory.Misson['Room'])
            {
                if (i.name == '过道采集')
                {
                    /* 进行删除 */
                    if (isInArray(i.data.relateRooms,addRoomName))
                        return `[cross] 房间${roomName}过道采集任务的房间清单已经存在房间${addRoomName}`
                    else
                    {
                        i.data.relateRooms.push(addRoomName)
                        return `[cross] 房间${roomName}过道采集任务的房间清单已经添加房间${addRoomName}！以下为房间清单：${i.data.relateRooms}`
                    }
                }
            }
            return `[cross] 房间${roomName}更改过道采集任务房间清单失败！请检查房间内是否已经存在该任务！`
        },
        /* 删除某个具体power任务 */
        delpower(roomName:string,disRoom:string):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[cross] 不存在房间${roomName}`
            for (var i of thisRoom.memory.Misson['Creep'])
            {
                if (i.name == 'power采集' && i.data.room == disRoom)
                {
                    if(thisRoom.DeleteMisson(i.id))
                        return `[cross] 删除${roomName}-->${disRoom}的power采集任务成功！`
                    else
                        return `[cross] 删除${roomName}-->${disRoom}的power采集任务失败！`
                }
            }
            return `[cross] 未找到${roomName}-->${disRoom}的power采集任务`
        },
        show(roomName:string):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[cross] 不存在房间${roomName}`
            var str = ''
            for (var i of thisRoom.memory.Misson['Room'])
            {
                if (i.name == '过道采集')
                {
                    str += `[cross] 房间${roomName}的过道采集任务详情配置如下：\n`
                    str += `     房间：${i.data.relateRooms}\n`
                    str += `     power:${i.data.power}\n`
                    str += `     deposit:${i.data.deposit}\n`
                    str += `     目前存在如下任务：`
                    /* 寻找目前存在的过道采集任务 */
                    for (var j of thisRoom.memory.Misson['Creep'])
                    {
                        if (j.name == 'power采集') str += `power采集任务 ${roomName}-->${j.data.room}  state:${j.data.role}\n`
                        if (j.name == 'deposit采集') str += `deposit采集任务 ${roomName}-->${j.data.rom}  state:${j.data.role}\n`
                    }
                    return str
                }
            }
            return `[cross] 房间${roomName}展示过道采集任务失败！请检查房间内是否已经存在该任务！`
        },
        /* 取消过道采集开关 */
        cancel(roomName:string):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[cross] 不存在房间${roomName}`
            for (var i of thisRoom.memory.Misson['Room'])
            {
                if (i.name == '过道采集')
                {
                    thisRoom.DeleteMisson(i.id)
                    return `[cross] 房间${roomName}已经取消过道采集任务！`
                }
            }
            return `[cross] 房间${roomName}取消过道采集任务失败！请检查房间内是否已经存在该任务！`
        },
    },

    /* 战争行为 */
    war:{
        double(roomName:string,disRoom:string,mType:'dismantle' | 'attack',num:number,shard:shardName = Game.shard.name as shardName):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[war] 不存在房间${roomName}`
            var thisTask = thisRoom.Public_Double(disRoom,shard,num,mType)
            thisTask.maxTime = 2
            if(thisRoom.AddMisson(thisTask)) return `[war] 双人小队 ${roomName} -> ${disRoom} 的 ${mType}任务挂载成功！`
            return `[war] 双人小队 ${roomName} -> ${disRoom} 的 ${mType}任务挂载失败！`
        },
        Cdouble(roomName:string,disRoom:string,mType:'dismantle'|'attack'):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[war] 不存在房间${roomName}`
            for (var i of thisRoom.memory.Misson['Creep'])
            {
                if (i.name == "双人小队" && i.data.disRoom == disRoom && mType)
                {
                    if (thisRoom.DeleteMisson(i.id)) return `[war] 双人小队 ${roomName} -> ${disRoom} 的 ${mType}任务删除成功！`
                }
            }
            return `[war] 双人小队 ${roomName} -> ${disRoom} 的 ${mType}任务删除失败！`
        },
        doubleChange(missonID:string,disRoom:string):string{
            for (var i in Game.creeps)
            {
                if (Game.creeps[i].memory.MissonData && Game.creeps[i].memory.MissonData.MisssonID == missonID)
                {
                    Game.creeps[i].memory.MissonData.data.disRoom = disRoom
                    console.log("已经更改",i,'的任务记忆！')
                }
            }
            return `[war] 双人小队任务房间已经修改!`
        },
        doubleRoomChange(roomName:string,disRoom:string,mType:'dismantle'|'attack',changeRoom:string):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[war] 不存在房间${roomName}`
            for (var i of thisRoom.memory.Misson['Creep'])
            {
                if (i.name == "双人小队" && i.data.disRoom == disRoom && mType)
                {
                    i.data.disRoom = changeRoom
                    return `[war] 双人小队 ${roomName} -> ${disRoom} 的 ${mType}任务 ----> ${changeRoom}！`
                }
            }
            return `[war] 双人小队 ${roomName} -> ${disRoom} 的 ${mType}任务删除失败！`
            return
        },
        doubleShow(roomName:string)
        {
            return
        },
        /* R型四人小队 */
        squardXR(roomName:string,disRoom:string,shard:shardName = Game.shard.name as shardName):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[war] 不存在房间${roomName}`
            var thisTask = thisRoom.Public_XR(disRoom,shard)
            thisTask.maxTime = 2
            if(thisRoom.AddMisson(thisTask)) return `[war] 四人小队 ${roomName} -> ${disRoom} 的 XR 任务挂载成功！`
            return `[war] 四人小队 ${roomName} -> ${disRoom} 的 XR 任务挂载失败！`
        },
        CsquardXR(roomName:string,disRoom:string):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[war] 不存在房间${roomName}`
            for (var i of thisRoom.memory.Misson['Creep'])
            {
                if (i.name == "X-R" && i.data.disRoom == disRoom)
                {
                    delete thisRoom.memory.squardData[i.id]
                    if (thisRoom.DeleteMisson(i.id))
                    {
                        return `[war] 四人小队 ${roomName} -> ${disRoom} 的 XR 任务删除成功！`
                    }
                }
            }
            return `[war] 四人小队 ${roomName} -> ${disRoom} 的 X-R 任务删除失败！`
        },
        /* 黄球拆迁 */
        dismantle(roomName:string,disRoom:string,CreepNum:number,shard:shardName,time:number = 1000):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[war] 未找到房间${roomName},请确认房间!`
            var thisTask = myRoom.Public_Dismantle(disRoom,shard,CreepNum,time)
            if (myRoom.AddMisson(thisTask)) return `[war] 黄球拆迁任务挂载成功! ${Game.shard.name}/${roomName} -> ${shard}/${disRoom}`
            return `[war] 黄球拆迁挂载失败!`
        },
        Cdismantle(roomName:string,disRoom:string,shard:shardName):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[war] 未找到房间${roomName},请确认房间!`
            for (var i of myRoom.memory.Misson['Creep'])
            {
                if (i.name == '黄球拆迁' && i.data.disRoom == disRoom && i.data.shard == shard)
                {
                    if (myRoom.DeleteMisson(i.id))
                    return `[war] 删除去往${shard}/${disRoom}的黄球拆迁任务成功！`
                }
            }
            return `[war] 删除去往${shard}/${disRoom}的黄球拆迁任务失败！`
        },
        /* 控制攻击 */
        control(roomName:string,disRoom:string,shard:shardName):string{
            var thisTask:MissionModel = {
                name:'控制攻击',
                range:'Creep',
                delayTick:99999,
                level:10,
                data:{
                    disRoom:disRoom,
                    shard:shard,
                    time:1200,
                    num:1
                },
                maxTime:3
            }
            thisTask.CreepBind = {'claim-attack':{num:1,bind:[]}}
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[war] 未找到房间${roomName},请确认房间!`
            if (myRoom.AddMisson(thisTask)) return `[war] 控制攻击任务挂载成功! ${Game.shard.name}/${roomName} -> ${shard}/${disRoom}`
            return `[war] 控制攻击挂载失败!`
        },
        Ccontrol(roomName:string,disRoom:string,shard:shardName):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[war] 未找到房间${roomName},请确认房间!`
            for (var i of myRoom.memory.Misson['Creep'])
            {
                if (i.name == '控制攻击' && i.data.disRoom == disRoom && i.data.shard == shard)
                {
                    if (myRoom.DeleteMisson(i.id))
                    return `[war] 删除去往${shard}/${disRoom}的控制攻击任务成功！`
                }
            }
            return `[war] 删除去往${shard}/${disRoom}的控制攻击任务失败！`
        },
        omkiller(roomName:string,disRoom:string,shard:shardName):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[war] 未找到房间${roomName},请确认房间!`
            var thisTask = myRoom.Public_OMKiller(disRoom,shard)
            if (myRoom.AddMisson(thisTask)) return `[war] om杀手任务挂载成功! ${Game.shard.name}/${roomName} -> ${shard}/${disRoom}`
            return `[war] om杀手挂载失败!`
        },
        Comkiller(roomName:string,disRoom:string,shard:shardName):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[war] 未找到房间${roomName},请确认房间!`
            for (var i of myRoom.memory.Misson['Creep'])
            {
                if (i.name == 'om杀手' && i.data.disRoom == disRoom && i.data.shard == shard)
                {
                    if (myRoom.DeleteMisson(i.id))
                    return `[war] 删除去往${shard}/${disRoom}的om杀手任务成功！`
                }
            }
            return `[war] 删除去往${shard}/${disRoom}的om杀手任务失败！`
        },
        
    },

    /* 外矿行为 */
    mine:{
        harvest(roomName:string,x:number,y:number,disRoom:string):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[mine] 不存在房间${roomName}`
            var thisTask = thisRoom.public_OutMine(roomName,x,y,disRoom)
            thisTask.maxTime = 8
            if(thisRoom.AddMisson(thisTask)) return `[mine] ${roomName} -> ${disRoom} 的外矿任务挂载成功！`
            return `[mine] ${roomName} -> ${disRoom} 的外矿任务挂载失败！`
        },
        Charvest(roomName:string,disRoom:string):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[mine] 不存在房间${roomName}`
            for (var i of thisRoom.memory.Misson['Creep'])
            {
                if (i.name == '外矿开采' && i.data.disRoom == disRoom)
                {
                    if (thisRoom.DeleteMisson(i.id)) return `[mine] ${roomName} -> ${disRoom} 的外矿任务删除成功！`
                }
            }
            return `[mine] ${roomName} -> ${disRoom} 的外矿任务删除失败！`
        },
        road(roomName:string):string{
            if (!Game.rooms[roomName]) return `[mine] 不存在相应视野`
            let roads = Game.rooms[roomName].find(FIND_STRUCTURES,{filter:(stru)=>{
                return stru.structureType == 'road'
            }})
            let cons = Game.rooms[roomName].find(FIND_CONSTRUCTION_SITES,{filter:(cons)=>{
                return cons.structureType == 'road'
            }})
            // 去除road记忆
            for (var i of Memory.outMineData[roomName].road)
            {
                let pos_ = unzipPosition(i) as RoomPosition
                if (pos_.roomName== roomName &&  !pos_.GetStructure('road'))
                {
                    let index = Memory.outMineData[roomName].road.indexOf(i)
                    Memory.outMineData[roomName].road.splice(index,1)
                }
            }
            let posList = []
            for (let r of roads) posList.push(zipPosition(r.pos))
            for (let c of cons) posList.push(zipPosition(c.pos))
            for (let p of posList)
            {
                if (!isInArray(Memory.outMineData[roomName].road,p))
                Memory.outMineData[roomName].road.push(p)
            }
            return `[mine] 已经更新房间${roomName}的外矿信息!`
        },
    },

    /* 刷墙行为 */
    repair:{
        add(roomName:string,Rtype:'global'|'special'|'nuker',num:number,boostType:number,vindicate:boolean):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[repair] 不存在房间${roomName}`
            for (var i of thisRoom.memory.Misson['Creep'])
            if (i.name == '墙体维护' && i.data.RepairType == Rtype)
            {
                return `[repair] 房间${roomName}已经存在类型为${Rtype}的刷墙任务了`
            }
            var thisTask = thisRoom.public_repair(Rtype,num,boostType,vindicate)
            if (thisRoom.AddMisson(thisTask))
            return `[repair] 房间${roomName}挂载类型为${Rtype}刷墙任务成功`
            return `[repair] 房间${roomName}挂载类型为${Rtype}刷墙任务失败！！`
        },
        remove(roomName:string,Rtype:'global'|'special'|'nuker'):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[repair] 不存在房间${roomName}`
            for (var i of thisRoom.memory.Misson['Creep'])
            if (i.name == '墙体维护' && i.data.RepairType == Rtype)
            {
                if (thisRoom.DeleteMisson(i.id))
                return `[repair] 房间${roomName}删除类型为${Rtype}刷墙任务成功！`
            }
            return `[repair] 房间${roomName}删除类型为${Rtype}刷墙任务失败！!`
        },
        round(roomName:string,x:number,y:number,range:number):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[repair] 不存在房间${roomName}`
            for (var i = x-range;i<x+range+1;i++)
            for (var j=y-range;j<y+range+1;j++)
            {
                if(!isInArray([0,1,48,49],i) &&!isInArray([0,1,48,49],j))
                {
                    if (Math.abs(i-x) == range || Math.abs(j-y) == range)
                    {
                        var pos_ = new RoomPosition(i,j,roomName)
                        pos_.createConstructionSite("rampart")
                    }
                }
            }
            return `[repair] 房间${roomName}包围型号墙壁创造完毕！`
        }
    },

    /* 工厂行为 */
    factory:{
        /* 设置房间内工厂等级,生产线 */
        set(roomName:string,level:number,work:'zip' | 'unzip',list:ResourceConstant[]):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[factory] 不存在房间${roomName}`
            if (!thisRoom.memory.factory) thisRoom.memory.factory = {}
            thisRoom.memory.factory.level = level
            thisRoom.memory.factory.list = list
            thisRoom.memory.factory.work = work
            return `[factory] 房间${roomName}的工厂等级已经设置为${level},工作为：${work} 生产内容为${list}！` 
        },
        del(roomName:string):string{
            var thisRoom = Game.rooms[roomName]
            if (!thisRoom) return `[factory] 不存在房间${roomName}`
            delete thisRoom.memory.factory
            for (var i of thisRoom.memory.Misson['Structure'])
            {
                delete thisRoom.memory.TerminalData[i.data.rType]
                if(i.name == '工厂加工' && thisRoom.DeleteMisson(i.id))
                return `[factory] 房间${roomName}的工厂记忆已经删除！` 
            }
            return `[factory] 房间${roomName}的工厂记忆和加工任务删除失败！` 
        },
        
    }
}